package com.weed.helper;

import com.weed.core.WeedException;
import java.io.*;
import java.util.*;

import org.jdom.*;
import org.jdom.input.*;
import com.weed.core.*;

/**
 * 函数说明:
 *     日志工厂，用于产生日志对象.
 *
 * 版本    修订人        说明
 * 0.1    Jason Zhuo    新增日志工厂
 */
public class XMLHelper {

   
    HashMap cache = new HashMap();//
    String filename;//文件名
    String location;//位置
    String type;//读取类型
    int label = -1;//xml如果有重复标签时，所处的位置。
    long lastmodifytime = 0;//文件最后一次更新时间。
    /**
     * 取XML文件指定位置的元素。
     */
    public static final String XML_CONTENT = "content";
    /**
     * 取XML指定位置的属性。
     */
    public static final String XML_ATTRIB = "attrib";

    public XMLHelper() {
    }

    /**
     * 解析字符串。
     * @return String[]
     * @throws WeedException
     */
    protected String[] convertLocation() throws WeedException {
        if (location == null || location.trim().equals("")) {
            throw new WeedException("Sorry,xmlpath invalid!");
        }
        return location.split("[.]");
    }

    /**
     * 读取配置文件。
     * @return Hashtable
     */
    public List loadXML() throws WeedException {
        if (filename == null || filename.trim().equals("")) {
            throw new WeedException("xml file[" + filename + "] not found!");
        }
        File file = new File(filename);
        if (!file.exists()) {
            return null;
        }
        if (file.isDirectory()) {
            return null;
        }
        String[] locations = convertLocation();
        SAXBuilder builder = null;
        InputStream is = null;
        List list = null;
        try {
            if (file.lastModified() != this.lastmodifytime || !cache.containsKey(location)) {//文件没有改变，
                this.lastmodifytime = file.lastModified();//最后一次更改时间
                builder = new SAXBuilder();
                is = new FileInputStream(filename);
                Document doc = builder.build(is);
                Element target = doc.getRootElement();
                for (int point = 0; point < locations.length; point++) {
                        if(locations.length == (point +1) && label != -1)
                            target = target.getChild(locations[point]);//取指定的元素
                        else
                            target = target.getChild(locations[point]);
                        
                }
                if (XML_ATTRIB.equalsIgnoreCase(type)) {
                    list = target.getAttributes();
                }else {
                    list = target.getContent();
                }
                cache.put(location, list);
            }else
                list = (List)cache.get(location);
        } catch (IOException ex) {
//            log.error(ex);
        } catch (JDOMException ex) {
  //          log.error(ex);
        } finally {
            if(file!=null)
                file = null;
            if (builder != null) {
                builder = null;
            }
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
                is = null;
            }

        }
        return list;
    }

    public String getFilename() {
        return filename;
    }

    public String getLocation() {
        return location;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public void setLocation(String location, String type) {
        this.location = location;
        this.type = type;
    }

    public static void main(String[] args) {
        XMLHelper helper = new XMLHelper();
        helper.setFilename("weed.xml");
        // helper.setLocation("impl.content");
        helper.setLocation("impl.interface.interface-class", XMLHelper.XML_CONTENT);
        try {
            List list = helper.loadXML();
            //helper.setLocation("impl.interface", XMLHelper.XML_CONTENT);
            //list = helper.loadXML();
            System.out.println("size=" + list.size());
            for (int i = 0; i < list.size(); i++) {
                System.out.println("map[" + i + "]=" + list.get(i));
            }
        } catch (WeedException ex) {
            ex.printStackTrace();
        }
    }
}
